const http = require('http');
const queryString = require('querystring');

const app = http.createServer((req, res) => {
  const url = req.url;
  const method = req.method;
  const path = url.split('?')[0];
  const query = queryString.parse(url.split('?')[1]);

  const resData = {
    url,
    method,
    path,
    query
  }

  res.setHeader('Content-type', 'application/json');

  if(method == 'GET') {
    res.end(
      JSON.stringify(resData)
    )
  }

  if(method == 'POST'){
    let postData = '';
    req.on('data', chunk => {
      postData += chunk;
    });
    req.on('data', () => {
      resData.postData = postData;

      res.end(JSON.stringify(resData))
    })
  }
 

})

app.listen(3000, ()=>{
  console.log('3000端口起来了');
})